package com.billdowney.service.impl.system.notice.sms;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.billdowney.dao.system.notice.SystemNoticeDao;
import com.billdowney.entity.system.notice.SystemNotice;
import com.billdowney.entity.system.notice.SystemNoticeType;
import com.billdowney.exception.BizException;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.system.notice.sms.SmsService;
import com.billdowney.service.util.project.SystemAppConst;
import com.billdowney.util.CommonUtil;
import com.billdowney.util.aliyun.sms.AliyunSmsUtil;
import com.billdowney.util.return_type.ReturnData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.json.JSONObject;

/**
 * 短信接口实现
 * 
 * @author 超级小富翁 - BillDowney
 * @date 2019年3月29日 下午4:12:26
 */
@Service
public class SmsServiceImpl extends BaseServiceImpl<SystemNoticeDao, SystemNotice, String> implements SmsService {

	// 阿里控制台keyId
	@Value(SystemAppConst.aliyunAccessKeyIdValue)
	private String accessKeyId;
	// 阿里控制台keySecret
	@Value(SystemAppConst.aliyunAccessKeySecretValue)
	private String accessKeySecret;
	// 阿里云账户
	@Value(SystemAppConst.aliyunUsernameValue)
	private String from;
	// jackson处理
	@Autowired
	private ObjectMapper objectMapper;

	@Resource(type = SystemNoticeDao.class)
	@Override
	public void setDao(SystemNoticeDao dao) {
		super.dao = dao;
	}

	@Override
	public ReturnData send(String phoneNumbers, String signName, String templateCode, JSONObject templateParam,
			String smsUpExtendCode, String outId) {
		CommonUtil.isNotEmpty(phoneNumbers, new BizException(10611));
		CommonUtil.isNotEmpty(signName, new BizException(10612));
		CommonUtil.isNotEmpty(templateCode, new BizException(10613));
		AliyunSmsUtil util = new AliyunSmsUtil();
		ReturnData out = new ReturnData();
		try {
			SendSmsResponse response = util.send(accessKeyId, accessKeySecret, phoneNumbers, signName, templateCode,
					templateParam, smsUpExtendCode, outId);
			if (!"OK".equals(response.getCode())) {
				out.setCode(0, response.getCode() + ":" + response.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException(10610);
		}
		this.saveSystemNotice(phoneNumbers, signName, templateCode, templateParam, smsUpExtendCode, outId);
		return out;
	}

	/**
	 * 保存系统通知
	 * 
	 * @author: 超级小富翁 - BillDowney
	 * @date: 2019年3月29日 下午4:49:38
	 */
	private void saveSystemNotice(String phoneNumbers, String signName, String templateCode, JSONObject templateParam,
			String smsUpExtendCode, String outId) {
		SystemNotice systemNotice = new SystemNotice();
		systemNotice.setSender(from);
		systemNotice.setSubject(signName);
		systemNotice.setContent(templateCode);
		systemNotice.setAddressee(phoneNumbers.split(","));
		systemNotice.setNoticeType(SystemNoticeType.sms);
		Map<String, Object> note = new HashMap<>();
		note.put("templateParam", templateParam);
		note.put("smsUpExtendCode", smsUpExtendCode);
		note.put("outId", outId);
		try {
			systemNotice.setNote(objectMapper.writeValueAsString(note));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		dao.save(systemNotice);
	}
}
